<?php

namespace common\extend\plugins;

use yii\db\Exception;
use yii\db\ActiveQuery;
use yii\db\Command;

/**
 * 提供基于ActiveQuery查询的分表
 * Class SubTableQuery
 * @package common\extend\plugins
 */
class SubTableQuery extends ActiveQuery
{
    use SubTableTrait;

    /**
     * SubTableQuery constructor.
     * @param $id
     * @param array $config
     * @throws Exception
     */
    public function __construct($id = null, $config = [])
    {
        static::setTableNameByKey($id);
        parent::__construct(get_called_class(), $config);
    }


    /**
     * rewrite 重写默认的createCommand，主要为了通过static::getDb()来替代默认的获取数据库连接
     * @param null $db
     * @return Command
     */
    public function createCommand($db = null): Command
    {
        if ($db === null) $db = static::getDb();
        return parent::createCommand($db);
    }
}